<?php
/* 
 * @fileName       : SiteController.php
 * @Copyright      : (c) 2012 Chienlv.
 * @software       : is developed by the technical team of Hung Viet .
 * @Blog           : levanchien.info.
 * @Yahoo          : levanchien_info.
 * @Email          : admin@levanchien.info.
 * @Mobile         : 0989092938.
 * @Description of : SiteController
 */
class SiteController extends Controller {
    /**
     * Declares class-based actions.
     */
    public function actions() {
        return array(
                // captcha action renders the CAPTCHA image displayed on the contact page
                'captcha'=>array(
                        'class'=>'CCaptchaAction',
                        'backColor'=>0xFFFFFF,
                        'minLength'=>5,
                        'maxLength'=>5,
                        'fontFile'=>_APP_PATH_.DIRECTORY_SEPARATOR."data".DIRECTORY_SEPARATOR.'ttf'.DIRECTORY_SEPARATOR.'arial.ttf',
                ),
                // page action renders "static" pages stored under 'protected/views/site/pages'
                // They can be accessed via: index.php?r=site/page&view=FileName
                'page'=>array(
                        'class'=>'CViewAction',
                ),
        );
    }

    /**
     * This is the default 'index' action that is invoked
     * when an action is not explicitly requested by users.
     */
    public function actionIndex() {
        // renders the view file 'protected/views/site/admin.php'
        // using the default layout 'protected/views/layouts/main.php'
        $dashboard_items = $this->getDashBoardItems();
        $this->render('index', array('dashboard_items'=>$dashboard_items));
    }

    /**
     *@des: get Dashboard items on home page
     * @return <type> array
     */
    public function getDashBoardItems()
    {
        $dashboard_items = array();
        
        $dashboard_items[] = array(
            "title"=>Yii::t('BackEnd', 'Email template'),
            "icon_url"=>Yii::app()->request->baseUrl."/css/admin/images/larg-icon/email.png",
            "url"=>"/emailTemplate/admin",
            'visible'=>UserAccess::checkAccess('emailTemplateAdmin')
        );
        
        //Account
        $dashboard_items[] = array(
            "title"=>Yii::t('BackEnd', 'Account manager'),
            "icon_url"=>Yii::app()->request->baseUrl."/css/admin/images/icons/1_48x48.png",
            "url"=>"/adminUser/admin",
            'visible'=>UserAccess::checkAccess('adminUserAdmin')
        );
        //Setting
        $dashboard_items[] = array(
            "title"=>Yii::t('BackEnd', 'Setting'),
            "icon_url"=>Yii::app()->request->baseUrl."/css/admin/images/icons/2_48x48.png",
            "url"=>"/config/setting",
            'visible'=>UserAccess::checkAccess('configSetting')
        );

        return $dashboard_items;
    }

    /**
     * Displays the login page
     */
    public function actionLogin() {
        $model = new LoginForm();

        // if it is ajax validation request
        if(isset($_POST['ajax']) && $_POST['ajax']==='login-form') {
            echo CActiveForm::validate($model);
            Yii::app()->end();
        }

        // collect user input data
        if(isset($_POST['LoginForm'])) {
            $model->attributes=$_POST['LoginForm'];
            // validate user input and redirect to the previous page if valid
            if($model->validate() && $model->login())
            {
                if(!Yii::app()->user->returnUrl)
                    $this->redirect(Yii::app()->request->baseUrl);
                else
                    $this->redirect(Yii::app()->user->returnUrl);
            }
        }
        // display the login form
        $this->layout = "login";
        $this->render('login',array('model'=>$model));
    }

    /**
     * Logs out the current user and redirect to homepage.
     */
    public function actionLogout() {
        // update user login status
        $user = AdminUser::model()->updateByPk(Yii::app()->user->getId(), array('status'=>AdminUser::STATUS_ACTIVED));
        Yii::app()->user->logout();
        $this->redirect(Yii::app()->homeUrl);
    }

    /**
     * change language
     */
    public function actionChangeLanguage() {
        $lang = $_REQUEST['lang'];
        if(isset(Yii::app()->params['langArray'][$lang])) {
            Yii::app()->setLanguage($lang);
            Yii::app()->session['_lang'] = $lang;
        }
        $this->redirect(Yii::app()->user->returnUrl);
    }
	public function actionMakeFriendlyUrl()
	{
        if(Yii::app()->request->getIsAjaxRequest()){
			$strName	=	(isset ($_POST['name'])?$_POST['name']:'');
			echo Common::makeFriendlyUrl($strName,true);
			Yii::app()->end();
		}else
			throw new CHttpException(400,'Invalid request. Please do not repeat this request again.');
	}
    public function actionLoadLanguageJs(){
        echo json_encode(include _APP_PATH_.'/protected/messages/'.Yii::app()->language."/JS.php");
        Yii::app()->end();
    }
    public function actionSelectLanguage() {
        if(Yii::app()->request->getIsAjaxRequest()){
            $lang = $_POST['lang_select'];
            if(isset(Yii::app()->params['langArray'][$lang])) {
                Yii::app()->setLanguage($lang);
                Yii::app()->session['_lang'] = $lang;
            }
            echo Yii::app()->request->getUrlReferrer();
       }
    }
}
?>
